package com.jfinal.weixin.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.ehcache.CacheKit;
import com.yy.tools.HttpRequest;
import com.yy.tools.MD5Util;
import com.yy.tools.OpenId;

public class IndexController extends Controller {
	public void index(){
		
		String path = PathKit.getRootClassPath();
		System.out.println(path);
		renderText("success");
	}
	
	public void device(){
		String hdevice_code = getPara("hdevice_code");
		String a = getPara("a");
		String c = getPara("c");
		String hospid = getPara("hospid");
		String m = getPara("m");
		setAttr("hdevice_code", hdevice_code);
		render("/old/Device/index.html");
	}
	
	
	public void bind(){
		String username = getPara("username");
		String password = getPara("password");
		String openid = getPara("openid");
        //engineer/loginweixin?heng_name=？？&heng_password=？？&heng_openid=？？&api_id=wx833&api_sign=wx885b3e4e8c7b4d89e01e
        
		String s=HttpRequest.sendGet("http://yiyikefu.com/engineer/loginweixin", "heng_name="+username+"&heng_password="+password+"&heng_openid="+openid+"&api_id=wx833&api_sign=wx885b3e4e8c7b4d89e01e");
		System.out.println(s);
        JSONObject jo = JSONObject.parseObject(s);
        String returncode =  jo.get("code")+"";
        if(returncode.equals("200")){
        	//绑定成功
        	render("/WEB-INF/html/deviceInfo.html");
        }else{
            renderText("绑定失败");
        }
	}
	

	public void unbind(){
		String code = getPara("code");
		if(code==null){
			renderText("no code");
		}else{
			String openid =OpenId.getOpenId(code);
			String heng_id =OpenId.getHengId(openid);
	        if(heng_id.equals("null")){
	        	//未绑定，跳转到登录页面
	        	renderText("未绑定");
	        }else{
	        	String isCz=HttpRequest.sendGet("http://yiyikefu.com/engineer/unbindOpenid", "openid="+openid+"&api_id=wx833&api_sign=wx885b3e4e8c7b4d89e01e");
	        	System.out.println(isCz);
	        	renderText(isCz);
	            JSONObject jo = JSONObject.parseObject(isCz);
	            String returncode =  jo.get("code")+"";
	            if(returncode.equals("200")){
	            	//解除绑定成功
		        	renderText("解除绑定成功");
	            }else{
	            	//解除绑定失败
		        	renderText("未绑定");
	            }
	        }
		}
	}
	
	public void getOpenId(){
		String code = getPara("code");
		if(code==null){
			renderText("no code");
		}else{
			/*System.out.println(code);
			String s=HttpRequest.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token", "appid="+PropKit.get("appId")+"&secret="+PropKit.get("appSecret")+"&code="+code+"&grant_type=authorization_code");
	        System.out.println(s);
	        
	        JSONObject jo = JSONObject.parseObject(s);
	        
	        String openid = jo.get("openid")+"";
	        
	        System.out.println(openid);
	        String isCz=HttpRequest.sendGet("http://yiyikefu.com/engineer/returnOpenid", "openid="+openid+"&api_id=wx833&api_sign=wx885b3e4e8c7b4d89e01e");
	        
	        System.out.println(isCz);
	        
	        JSONArray ja = JSONArray.parseArray(isCz);
	        JSONObject jo1 = ja.getJSONObject(0);
	        String heng_id = jo1.get("heng_id")+"";
	        System.out.println(heng_id);
	        */

			String openid =OpenId.getOpenId(code);
			String heng_id =OpenId.getHengId(openid);
	        if(heng_id.equals("null")){
	        	//未绑定，跳转到登录页面
	        	setAttr("openid", openid);
	        	render("/WEB-INF/html/login.html");
	        }else{
	        	//已经绑定
	    		/*setSessionAttr("signature", getPara("signature"));
	    		setSessionAttr("nonce", getPara("nonce"));
	    		setSessionAttr("timestamp", getPara("timestamp"));*/
	        	setAttr("appId", PropKit.get("appId"));
	        	render("/WEB-INF/html/deviceInfo.html");
	        }
		}
	}
}
